package cn.imnaer.service.rpc.share.aop;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import cn.imnaer.service.persistence.model.PushMessage;
import cn.imnaer.service.persistence.model.SharePhotoComment;
import cn.imnaer.service.rpc.push.dao.MessagePushActiveRecord;
import cn.imnaer.service.rpc.share.process.SharePhotoAdapter;
import cn.imnaer.service.rpc.share.process.SharePhotoContext;
import cn.imnaer.service.rpc.share.process.SharePhotoService;
import cn.imnaer.service.rpc.userinfo.impl.UserPermissionComponentRpcApiImpl;
import cn.imnaer.service.rpc.userinfo.process.UserModuleService;

public class SharePhotoComponentPushService {
	public void pushComment(SharePhotoComment comment) {
		String sharePhotoItemId = comment.getSharePhotoItemId();
		List<String> receivers = new ArrayList<>();
		SharePhotoContext context = new SharePhotoContext();
		context.setSharePhotoItemId(sharePhotoItemId);
		SharePhotoAdapter adapter = (SharePhotoAdapter) SharePhotoService.getInstance(context).getAdapter();
		Collection<String> userIds = adapter.getItemInvolvedUsers(sharePhotoItemId);
		/**
		 * author friends
		 */
		List<String> friends = new UserPermissionComponentRpcApiImpl().calculateViewPermission(comment.getUserId());
		if(comment.getReplyUserId()!=null&&!comment.getReplyUserId().isEmpty()){
			List<String> replyUserFriends = new UserPermissionComponentRpcApiImpl().calculateViewPermission(comment.getReplyUserId());
			friends.retainAll(replyUserFriends);
		}
		friends.retainAll(userIds);
		String author = adapter.getAuthor();
		receivers.addAll(friends);
		receivers.add(author);
		HashSet<String> set = new HashSet<>(receivers);
		List<PushMessage> messages = new ArrayList<>(set.size());
		Iterator<String> iterator = set.iterator();
		/**
		 * 有一种情况，就是自己给自己评论或者点赞。就是不需要推送。
		 */
		while (iterator.hasNext()) {
			String receiver = iterator.next();
			if (!receiver.equals(comment.getUserId())) {
				PushMessage message = new PushMessage();
				message.setIdType("SHARE_PHOTO");
				message.setMessageType("REPLY");
				message.setContent(comment.getContent());
				message.setSender(comment.getUserId());
				message.setCreateTime(comment.getCreateTime());
				message.setImage(adapter.getImage());
				message.setUserName(comment.getUserName());
				message.setId(comment.getSharePhotoItemId());
				message.setReceiver(receiver);
				messages.add(message);
			}

		}
		MessagePushActiveRecord.getInstance().batchInsert(messages);
	}

	public void pushSupport(String sharePhotoItemId, String userId, long createTime) {
		List<String> receivers = new ArrayList<>();
		SharePhotoContext context = new SharePhotoContext();
		context.setSharePhotoItemId(sharePhotoItemId);
		SharePhotoAdapter adapter = null;
		try {
			adapter = (SharePhotoAdapter) SharePhotoService.getInstance(context).getAdapter();
		} catch (NullPointerException e) {

			e.printStackTrace();
		}

		Collection<String> userIds = adapter.getItemInvolvedUsers(sharePhotoItemId);
		List<String> friends = new UserPermissionComponentRpcApiImpl().calculateViewPermission(userId);
		friends.retainAll(userIds);
		String author = adapter.getAuthor();
		receivers.add(author);
		String userName = new UserModuleService().getUserName(userId);
		receivers.addAll(friends);
	
		HashSet<String> set = new HashSet<>(receivers);
		List<PushMessage> messages = new ArrayList<>(set.size());
		Iterator<String> iterator = set.iterator();
		while (iterator.hasNext()) {
			String receiver = iterator.next();
			if (!receiver.equals(userId)) {
				PushMessage message = new PushMessage();
				message.setIdType("SHARE_PHOTO");
				message.setMessageType("SUPPORT");
				message.setId(sharePhotoItemId);
				message.setSender(userId);
				message.setContent("");
				message.setImage(adapter.getImage());
				message.setCreateTime(createTime);
				message.setReceiver(receiver);
				message.setUserName(userName);
				messages.add(message);
			}
		}
		MessagePushActiveRecord.getInstance().batchInsert(messages);
	}

}
